/*
* 添加生产记录
* */

import 'package:center_beijing/api/component_api.dart';
import 'package:center_beijing/common/QRScan.dart';
import 'package:center_beijing/common/custom_floatingAction_buttonLocation.dart';
import 'package:center_beijing/common/enums.dart';
import 'package:center_beijing/common/logutil.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/config/routeConfig.dart';
import 'package:center_beijing/models/GetxControllerOne.dart';
import 'package:center_beijing/widgets/custom_loading_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class AddProductionRecord extends StatefulWidget {
  @override
  _AddProductionRecordState createState() => _AddProductionRecordState();
}

class _AddProductionRecordState extends State<AddProductionRecord>
    with SingleTickerProviderStateMixin {
  CustomLoading loading = CustomLoading.withMessage('请稍候...');
  List procedure = [];
  int currentStageIndex = 0;

  @override
  void initState() {
    super.initState();
    this.getStage();
  }

  /*
  * 获取构件生产工序
  * */
  getStage() async {
    loading.show(delay: 30);
    var temIndex = 0;
    var result =
        await getWorkStage(Get.find<GetXControllerOne>().componentDetail['id']);
    loading.dismiss();
    if (result != null && result.length > 0) {
      for (var i = 0; i < result.length; i++) {
        if (result[i]['id'] ==
            Get.find<GetXControllerOne>().componentDetail['workstageStatus']) {
          temIndex = i + 1;
          break;
        }
      }
      this.setState(() {
        procedure = result;
        currentStageIndex = temIndex;
      });
    }
    LogUtil.d(result);
  }

  @override
  void dispose() {
    super.dispose();
  }

  getRecord(index,flowList) {
    return IntrinsicHeight(
      child: Row(
        children: [
          Container(
            color: Styles.WhiteGray,
            width: 30.w,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                index == 0
                    ? Container(
                        height: 24.h,
                      )
                    : Container(
                        color: Styles.DetailColor20,
                        width: 1,
                        height: 23.h,
                      ),
                Image.asset(
                  index == 0
                      ? 'assets/images/already_gou.png'
                      : 'assets/images/quan.png',
                  width: 12.w,
                  height: 12.h,
                  fit: BoxFit.contain,
                ),
                index == 0
                    ? Container(
                        color: Styles.DetailColor20,
                        width: 1,
                        height: 23.h,
                      )
                    : (index != procedure.length - 1
                        ? Container(
                            color: Styles.DetailColor20,
                            width: 1,
                            height: 24.h,
                          )
                        : Container(
                            height: 24.h,
                          ))
              ],
            ),
          ),
          Expanded(
              child: GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () async {
              if (index < currentStageIndex) {
                var waterId = '';
                flowList.forEach((item) {
                  if (item['workstageId'] == procedure[index]['id']) {
                    waterId = item['waterId'];
                  }
                });
                if (waterId != '') {
                  Get.toNamed(RouteConfig.productionRecordDetail,
                      arguments: waterId);
                }
              } else if (index == currentStageIndex) {
                if(Get.find<GetXControllerOne>().componentDetail['state'] == 3){
                  Fluttertoast.showToast(msg: '该构件已报废');
                }else{
                  if (Get.find<GetXControllerOne>().componentDetail['state'] ==
                      1) {
                    var result = await Get.toNamed(RouteConfig.completeProcess,
                        arguments: {
                          'compDetail':
                          Get.find<GetXControllerOne>().componentDetail,
                          'procedure': procedure[index],
                          'type':index
                        });
                    if(result!=null){
                      Get.back();
                    }
                    // this.getStage();
                  } else {
                    Fluttertoast.showToast(msg: '构件状态非正常');
                  }
                }
              } else {}
            },
            child: Container(
              padding: EdgeInsets.fromLTRB(Styles.spacing16, Styles.spacing8,
                  Styles.spacing16, Styles.spacing8),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Row(
                    children: [
                      Text(
                        procedure[index]['workStageName'] != null
                            ? procedure[index]['workStageName']
                            : '--',
                        style: TextStyle(
                            color: Styles.DetailColor,
                            fontSize: Styles.NormalFont),
                      ),
                      Container(
                        margin: EdgeInsets.only(left: Styles.spacing8),
                        padding: EdgeInsets.fromLTRB(
                            Styles.spacing6, 0, Styles.spacing6, 0),
                        decoration: BoxDecoration(
                            borderRadius:
                                BorderRadius.all(Radius.circular(10.0.r)),
                            color: currentStageIndex == index
                                ? Styles.BlueColor
                                : Styles.GrayButtonColor),
                        child: Text(
                          index < currentStageIndex
                              ? '已执行'
                              : (index == currentStageIndex ? '可执行' : '待执行'),
                          style: TextStyle(
                              color: Styles.ShareWhite,
                              fontSize: Styles.SmallFont),
                        ),
                      )
                    ],
                  ),
                  index < currentStageIndex
                      ? Container(
                          child: Text(
                            '详情',
                            style: TextStyle(
                                color: Styles.BlueColor,
                                fontSize: Styles.NormalFont),
                          ),
                        )
                      : Image.asset(
                          'assets/images/right_arrow.png',
                          width: 14.w,
                          height: 14.w,
                          fit: BoxFit.contain,
                          color: Styles.LeftTitleColor,
                        )
                ],
              ),
            ),
          ))
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<GetXControllerOne>(
      init: GetXControllerOne(),
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(
            title: Text('添加生产记录'),
          ),
          body: Container(
            color: Styles.MainBackgroundColor,
            padding: EdgeInsets.fromLTRB(
                0, 0, 0, MediaQuery.of(context).padding.bottom),
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.fromLTRB(
                        Styles.spacing16, Styles.spacing8, 0, Styles.spacing8),
                    padding: EdgeInsets.fromLTRB(Styles.spacing8,
                        Styles.spacing8, Styles.spacing8, Styles.spacing8),
                    decoration: BoxDecoration(
                        gradient: LinearGradient(
                      colors: [
                        Colors.blue,
                        Styles.BlueColor,
                      ],
                    )),
                    child: RichText(
                      text: TextSpan(children: [
                        TextSpan(
                          text: '•',
                          style: TextStyle(
                              fontSize: Styles.Middle16Font,
                              color: Styles.ShareWhite),
                        ),
                        TextSpan(
                          text: '  ',
                          style: TextStyle(
                              fontSize: Styles.Middle16Font,
                              color: Styles.ShareWhite),
                        ),
                        TextSpan(
                          text: '生产工序',
                          style: TextStyle(
                              fontSize: Styles.NormalFont,
                              color: Styles.ShareWhite),
                        ),
                      ]),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.fromLTRB(
                        Styles.spacing8, 0, Styles.spacing8, 0),
                    child: Column(
                      children:
                          this.procedure.asMap().keys.map<Widget>((index) {
                        return this.getRecord(index,controller.dataSource);
                      }).toList(),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.fromLTRB(
                        Styles.spacing16, Styles.spacing8, 0, Styles.spacing8),
                    padding: EdgeInsets.fromLTRB(Styles.spacing8,
                        Styles.spacing8, Styles.spacing8, Styles.spacing8),
                    decoration: BoxDecoration(
                        gradient: LinearGradient(
                      colors: [
                        Colors.blue,
                        Styles.BlueColor,
                      ],
                    )),
                    child: RichText(
                      text: TextSpan(children: [
                        TextSpan(
                          text: '•',
                          style: TextStyle(
                              fontSize: Styles.Middle16Font,
                              color: Styles.ShareWhite),
                        ),
                        TextSpan(
                          text: '  ',
                          style: TextStyle(
                              fontSize: Styles.Middle16Font,
                              color: Styles.ShareWhite),
                        ),
                        TextSpan(
                          text: '质量状态',
                          style: TextStyle(
                              fontSize: Styles.NormalFont,
                              color: Styles.ShareWhite),
                        ),
                      ]),
                    ),
                  ),
                  Container(
                    child: Column(
                      children: [
                        GestureDetector(
                          onTap: () {
                            if(controller.componentDetail['state'] == 3){
                              Fluttertoast.showToast(msg: '该构件已报废');
                            }else{
                              if (controller.componentDetail['state'] != 1) {
                                Get.toNamed(RouteConfig.addQualityStatus,
                                    arguments: {
                                      'compDetail': controller
                                          .componentDetail,
                                      'type': StatusType.normal
                                    });
                              }
                            }

                          },
                          child: Container(
                            margin: EdgeInsets.only(top: Styles.spacing8),
                            color: Styles.GrayLight,
                            padding: EdgeInsets.fromLTRB(
                                Styles.spacing16,
                                Styles.spacing16,
                                Styles.spacing16,
                                Styles.spacing16),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                      height: 20.h,
                                      width: 4,
                                      color: Styles.BlueColor,
                                      margin: EdgeInsets.only(
                                          right: Styles.spacing8),
                                    ),
                                    Text(
                                      '正常',
                                      style: TextStyle(
                                          color: Styles.DetailColor,
                                          fontSize: Styles.Middle16Font),
                                    ),
                                  ],
                                ),
                                controller.componentDetail['state'] == 1
                                    ? Container(
                                        padding: EdgeInsets.fromLTRB(
                                            Styles.spacing6,
                                            0,
                                            Styles.spacing6,
                                            0),
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(10.0.r)),
                                            color: Styles.BlueColor),
                                        child: Text(
                                          '当前',
                                          style: TextStyle(
                                              color: Styles.ShareWhite,
                                              fontSize: Styles.SmallFont),
                                        ),
                                      )
                                    : Image.asset(
                                        'assets/images/right_arrow.png',
                                        width: 14.w,
                                        height: 14.w,
                                        fit: BoxFit.contain,
                                        color: Styles.LeftTitleColor,
                                      )
                              ],
                            ),
                          ),
                        ),
                        GestureDetector(
                          onTap: () {
                            if(controller.componentDetail['state'] == 3){
                              Fluttertoast.showToast(msg: '该构件已报废');
                            }else{
                              if (controller.componentDetail['state'] != 2) {
                                Get.toNamed(RouteConfig.addQualityStatus,
                                    arguments: {
                                      'compDetail': controller
                                          .componentDetail,
                                      'type': StatusType.repair
                                    });
                              }
                            }
                          },
                          child: Container(
                            margin: EdgeInsets.only(top: Styles.spacing8),
                            color: Styles.GrayLight,
                            padding: EdgeInsets.fromLTRB(
                                Styles.spacing16,
                                Styles.spacing16,
                                Styles.spacing16,
                                Styles.spacing16),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                      height: 20.h,
                                      width: 4,
                                      color: Styles.RedColor,
                                      margin: EdgeInsets.only(
                                          right: Styles.spacing8),
                                    ),
                                    Text(
                                      '维修',
                                      style: TextStyle(
                                          color: Styles.DetailColor,
                                          fontSize: Styles.Middle16Font),
                                    ),
                                  ],
                                ),
                                controller.componentDetail['state'] == 2
                                    ? Container(
                                        padding: EdgeInsets.fromLTRB(
                                            Styles.spacing6,
                                            0,
                                            Styles.spacing6,
                                            0),
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(10.0.r)),
                                            color: Styles.BlueColor),
                                        child: Text(
                                          '当前',
                                          style: TextStyle(
                                              color: Styles.ShareWhite,
                                              fontSize: Styles.SmallFont),
                                        ),
                                      )
                                    : Image.asset(
                                        'assets/images/right_arrow.png',
                                        width: 14.w,
                                        height: 14.w,
                                        fit: BoxFit.contain,
                                        color: Styles.LeftTitleColor,
                                      )
                              ],
                            ),
                          ),
                        ),
                        GestureDetector(
                          onTap: () {
                            if (controller.componentDetail['state'] != 3) {
                              Get.toNamed(RouteConfig.addQualityScrapStatus,
                                  arguments: {
                                    'compDetail': controller
                                        .componentDetail,
                                    'type': StatusType.scrap
                                  });
                            }
                          },
                          child: Container(
                            margin: EdgeInsets.only(top: Styles.spacing8),
                            color: Styles.GrayLight,
                            padding: EdgeInsets.fromLTRB(
                                Styles.spacing16,
                                Styles.spacing16,
                                Styles.spacing16,
                                Styles.spacing16),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                      height: 20.h,
                                      width: 4,
                                      color: Styles.DetailColor,
                                      margin: EdgeInsets.only(
                                          right: Styles.spacing8),
                                    ),
                                    Text(
                                      '报废',
                                      style: TextStyle(
                                          color: Styles.DetailColor,
                                          fontSize: Styles.Middle16Font),
                                    ),
                                  ],
                                ),
                                controller.componentDetail['state'] == 3
                                    ? Container(
                                        padding: EdgeInsets.fromLTRB(
                                            Styles.spacing6,
                                            0,
                                            Styles.spacing6,
                                            0),
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(10.0.r)),
                                            color: Styles.BlueColor),
                                        child: Text(
                                          '当前',
                                          style: TextStyle(
                                              color: Styles.ShareWhite,
                                              fontSize: Styles.SmallFont),
                                        ),
                                      )
                                    : Image.asset(
                                        'assets/images/right_arrow.png',
                                        width: 14.w,
                                        height: 14.w,
                                        fit: BoxFit.contain,
                                        color: Styles.LeftTitleColor,
                                      )
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
            floatingActionButtonLocation: CustomFloatingActionButtonLocation(FloatingActionButtonLocation.endFloat,),
            floatingActionButtonAnimator: NoScalingAnimation(),
            floatingActionButton: FloatingActionButton(
                child: Image.asset(
                  'assets/images/saoyisao.png',
                  fit: BoxFit.contain,
                ),
                onPressed: () async {
                  var result = await QrScan.scan(context);
                  if(result!=null){
                    if(result['type']==1){
                      Get.toNamed(RouteConfig.productionDetail,arguments: result['code']);
                    }else{
                      showDialog(
                          barrierDismissible: false,
                          context: context,
                          builder: (BuildContext context) {
                            return AlertDialog(
                              title: Text('注意'),
                              content: Text('请扫描构件二维码！'),
                              actions: <Widget>[
                                new Container(
                                  child: FlatButton(
                                    child: new Text(
                                      "确定",
                                      style: TextStyle(color: Colors.red),
                                    ),
                                    onPressed: () {
                                      Navigator.pop(context);
                                    },
                                  ),
                                ),

                              ],
                            );
                          });
                    }
                  }
                },
                backgroundColor: Colors.transparent)
        );
      },
    );
  }
}
